import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

/// FileName shared_preferences_manager
///
/// @Author lax
/// @Date 2023/6/16
///
/// @Description

class SharedPreferencesManager {
  final SharedPreferences store;

  SharedPreferencesManager._internal(this.store);

  static Future<SharedPreferencesManager> _getInstance() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return SharedPreferencesManager._internal(preferences);
  }

  /// 获取字符串
  static Future<String?> getString<T>(
      {required T key, Function(String? value)? onFinish}) async {
    final instance = await SharedPreferencesManager._getInstance();
    final obj = instance.store.get(key.toString());
    final value = obj is String ? obj : null;
    onFinish?.call(value);
    return value;
  }

  /// 设置字符串
  static Future<bool> setString<T>(
      {required T key, required String? value}) async {
    if (value == null) {
      return remove(key: key);
    } else {
      final instance = await SharedPreferencesManager._getInstance();
      return instance.store.setString(key.toString(), value);
    }
  }

  /// 获取字符串数组
  static Future<List<String>?> getStringList<T>(
      {required T key, Function(List<String>? value)? onFinish}) async {
    final str = await getString(key: key.toString());
    List<String>? value;
    if (str != null) {
      try {
        final obj = jsonDecode(str);
        if (obj is List) {
          value = obj.cast<String>();
        }
      } catch (_) {}
    }
    onFinish?.call(value);
    return value;
  }

  /// 设置字符串数组
  static Future<bool> setStringList<T>(
      {required T key, required List<String> value}) async {
    return setString(key: key, value: jsonEncode(value));
  }

  static Future<bool> remove<T>({required T key}) async {
    final instance = await SharedPreferencesManager._getInstance();
    return instance.store.remove(key.toString());
  }

  static Future<List<bool>> removeMultiple<T>({required List<T> keys}) async {
    List<bool> list = [];
    final instance = await SharedPreferencesManager._getInstance();
    for (var key in keys) {
      final result = await instance.store.remove(key.toString());
      list.add(result);
    }
    return list;
  }

  /// 获取bool
  static Future<bool?> getBool<T>(
      {required T key, Function(bool? value)? onFinish}) async {
    final value = await getString(key: key);
    onFinish?.call(value == '1');
    return value == null ? null : value == '1';
  }

  /// 设置bool
  static Future<bool> setBool<T>({required T key, required bool? value}) {
    return setString(
        key: key, value: value == null ? null : (value ? '1' : '0'));
  }

  /// 获取Map
  static Future<Map?> getMap<T>(
      {required T key, Function(Map? value)? onFinish}) async {
    final value = await getString(key: key);
    Map? json;
    if (value != null) {
      json = jsonDecode(value);
    }
    onFinish?.call(json);
    return json;
  }

  /// 设置Map
  static Future<bool> setMap<T>({required T key, required Map? value}) {
    final json = value != null ? jsonEncode(value) : null;
    return setString(key: key, value: json);
  }
}
